<template>
  <div>
    <el-button
      :plain="circle"
      :type="type"
      size="small"
      icon="el-icon-edit"
      @click="click"
      :circle="circle"
      >{{ msg }}</el-button
    >
    <el-dialog :title="title" :visible.sync="dialogFlag">
      <el-form
        :rules="rules"
        :model="form"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="title === '新增用户'" label="密码" prop="password">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称" prop="permission_group_id">
          <el-select v-model="form.permission_group_id" placeholder="请选择">
            <el-option
              v-for="item in simple"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.introduction"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="dialogFlag = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/base/permissions'
export default {
  name: 'addFrom',
  components: {},
  props: {
    msg: {
      type: String
    },
    type: {
      type: String,
      default: 'success'
    },
    circle: {
      type: Boolean,
      default: false
    },
    title: {
      type: String
    },
    form: {
      type: Object
    }
  },
  data () {
    return {
      dialogFlag: false,
      rules: {
        username: [{ required: true, message: '用户名不能为空' }],
        email: [{ required: true, message: '邮箱不能为空' }],
        password: [{ required: true, message: '密码不能为空' }],
        role: [{ required: true, message: '角色不能为空' }]
      },
      simple: []
    }
  },
  computed: {},
  watch: {},
  methods: {
    async submitForm () {
      await this.$refs.form.validate()
      await this.$emit('submitForm', this.form)
      this.dialogFlag = false
      this.$refs.form.resetFields()
    },
    async click () {
      const { data } = await simple()
      this.simple = data
      this.dialogFlag = true
      this.$emit('Open')
    }
  }
}
</script>

<style scoped></style>
